package com.lzy.concertticket.util;


import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

@Component
public class UrlUtil {

    @Value("${server.port}")
    private String port;

    @SneakyThrows
    public  String getResourceUrlPath(String urlSuffix) {
        if (urlSuffix.charAt(0) != '/') {
            urlSuffix = '/' + urlSuffix;
        }
        //  获取本机网络ip
        InetAddress inetAddress = InetAddress.getLocalHost();
        if (inetAddress.isLoopbackAddress()) {
            return "http://127.0.0.1:"+port + urlSuffix;
        } else {
            return "http://"+inetAddress.getHostAddress()+":"+port + urlSuffix;
        }
    }

    @SneakyThrows
    public  String getResourceImageUrlPath(String urlSuffix) {
        if (urlSuffix.charAt(0) != '/') {
            urlSuffix = '/' + urlSuffix;
        }
        urlSuffix = "/images" + urlSuffix;
        //  获取本机网络ip
        InetAddress inetAddress = InetAddress.getLocalHost();
        if (inetAddress.isLoopbackAddress()) {
            return "http://127.0.0.1:"+port + urlSuffix;
        } else {
            return "http://"+inetAddress.getHostAddress()+":"+port + urlSuffix;
        }
    }


}


